/**
 *
 * Created by vimer on 15/8/25.
 */


function getTaskList(){
    var taskConfig =  JSON.parse(KeyValue.get("config"));
    var timer ;
    var TaskList = [
        {
            id : 1,
            name : "mpu6000 基础测试" ,
            startCommand : function(){
                return "mpu6000 start \nmpu6000 factorytest\n";
            },
            status: function(data,callback){

                if(data.indexOf("PASSED") === 0){
                    callback(TaskStatus.GOOD);
                }
                else if(data.indexOf("FAIL") === 0){
                    callback(TaskStatus.FAIL);
                }
                else {
                    callback(TaskStatus.RUNNING);

                }
            },
            enable : taskConfig['1'].enable
        },
        {
            id : 2,
            name : "mpu6000 Bad Acc" ,
            startCommand : function(){
                return "mpu6000 start \nmpu6000 info\n";
            },
            status: function(data,callback){
                function getValue(str,target){
                    var index = data.indexOf(target);

                    if(index > -1) {
                        var value = data.substr(index + target.length, data.indexOf("event"));
                        value = value.trim();
                        console.log(value)
                        return value;
                    }
                    return 0;
                }
                if(getValue(data,"mpu6000_bad_registers: ") > 0 ||
                    getValue(data,"mpu6000_bad_transfers: ") > 0   ){
                    callback(TaskStatus.FAIL);
                }
                else if ( data.indexOf("temperature") != -1 ){
                    callback(TaskStatus.GOOD) ;
                }
                else {
                    callback(TaskStatus.RUNNING);
                }

            },
            repeat : taskConfig[2].option.count,
            enable : taskConfig[2].enable
        },
        {
            id : 3,
            name : "ms5611" ,
            startCommand : function(){
                return "ms5611 start\nms5611 calibrate\nms5611 factorytest " + taskConfig[3].option.time + "\n";
            },
            status: function(data,callback){
                var target = "variance:";
                var index = data.indexOf(target);

                if(index > -1){
                    var value = data.substr(index + target.length, data.length - index - target.length);
                    value = value.trim();
                    value = value.replace('-' ,"");
                    value =  parseFloat(value);
                    if( value  - 0.5  < 0){
                        callback(TaskStatus.GOOD);
                    }
                    else {
                        callback(TaskStatus.FAIL);
                    }
                    return;
                }
                callback(TaskStatus.RUNNING);
            },
            enable : taskConfig[3].enable
        },
        {
            id : 4,
            name : "lsm303d Bad Acc" ,
            startCommand : function(){
                return "lsm303d start \nlsm303d info\n";
            },
            status: function(data,callback){
                function getValue(str,target){
                    var index = data.indexOf(target);

                    if(index > -1) {
                        var value = data.substr(index + target.length, data.indexOf("event"));
                        value = value.trim();
                        console.log(value)
                        return value;
                    }
                    return 0;
                }
                if(getValue(data,"lsm303d_bad_registers: ") > 0 ||
                    getValue(data,"lsm303d_bad_values: ") > 0   ){
                    callback(TaskStatus.FAIL);
                }
                else if ( data.indexOf("temperature") != -1 ){
                    callback(TaskStatus.GOOD) ;
                }
                else {
                    callback(TaskStatus.RUNNING);
                }

            },
            repeat : taskConfig[4].option.count,
            enable : taskConfig[4].enable
        },
        {
            id : 5,
            name : "LED 测试 " ,
            startCommand : function(){
                return "rgbled start \nrgbled test\n";
            },
            status: function(data,callback){
                if(!timer){
                    timer = setTimeout(function(){
                        timer = undefined;
                        callback(TaskStatus.GOOD) ;
                    },1000  *  taskConfig[5].option.time);

                }

            },
            endCommand : function(){
                return "rgbled stop \n";
            },
            repeat : 1,
            enable : taskConfig[5].enable
        },
        {
            id : 6,
            name : "Buzzer 测试 " ,
            startCommand : function(){
                return "tone_alarm start\n";
            },
            status: function(data,callback){
                if(!timer){
                    timer = setTimeout(function(){
                        timer = undefined;
                        callback(TaskStatus.GOOD) ;
                    },1000  *  taskConfig[6].option.time);

                }

            },
            endCommand : function(){
                return "tone_alarm stop \n";
            },
            repeat : 1,
            enable : taskConfig[6].enable
        },
    ];
    return TaskList;

}
